package org.lmind.example.common.service.mail;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.lmind.example.common.api.Mail;

public class Test {
	public static void main(String[] args) throws Exception {
		//sendhtml(args);
		se();
	}
	
	public static void se() throws Exception {
		DefaultMailService mailService = new DefaultMailService();
		mailService.setHost("smtp.qq.com");
		mailService.setUser("5890949");
		mailService.setPassword("koffighter");
		Mail mail = mailService.newMail();
		mail.setSubject("my mail");
		mail.setBody("cool", "text/text");
		mailService.sendMail(mail, "刘勉<5890949@qq.com>", "5890949@qq.com");
	}

	public static void sendhtml(String[] argv) throws Exception {

		String to = "5890949@qq.com";
		String subject = "lmind";
		String from = "5890949@qq.com ";
		String cc = null;
		String bcc = null;
		String mailhost = "smtp.qq.com";
		boolean debug = true;
		String username = "5890949";
		String password = "koffighter";
		Properties props = System.getProperties();
		props.put("mail.smtp.auth", "true");
		SMTPAuth auth = new SMTPAuth(username, password);
		// could use Session.getTransport() and Transport.connect()
		// assume we 're using SMTP
		if (mailhost != null)
			props.put("mail.smtp.host", mailhost);

		// Get a Session object
		Session session = Session.getDefaultInstance(props, auth);
		if (debug)
			session.setDebug(true);

		// construct the message
		Message msg = new MimeMessage(session);
		if (from != null)
			msg.setFrom(new InternetAddress(from));
		else
			msg.setFrom();

		msg.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(to, false));
		if (cc != null)
			msg.setRecipients(Message.RecipientType.CC,
					InternetAddress.parse(cc, false));
		if (bcc != null)
			msg.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(bcc, false));

		msg.setSubject(subject);

		MimeBodyPart mbp1 = new MimeBodyPart();
		String html = " <!DOCTYPE   HTML   PUBLIC   \"-//W3C//DTD   HTML   4.0   Transitional//EN\"> "
				+ " <html> "
				+ " <head> <title> </title> </head> "
				+ " <body> "
				+ " <b>   see   the   following   jpg   :   it   is   a   car! </b> <br> "
				+ " <a   href=http://www.a.com/a.jsp> hello </a> <br> "
				+ " <IMG   SRC=cid:mail.jpg   width=80%   height=60%> <br> "
				+ " <b>   end   of   jpg </b> " + " </body> " + " </html> ";

		mbp1.setContent(html, "text/html");

		FileDataSource fds = new FileDataSource("d:/mail.jpg");
		MimeBodyPart mbp2 = new MimeBodyPart();
		mbp2.setFileName(fds.getName());
		mbp2.setText("This   is   a   beautiful   car   ! ");
		mbp2.setDataHandler(new DataHandler(fds));
		mbp2.setHeader("Content-ID", "<" + fds.getName() + ">");

		MimeMultipart mp = new MimeMultipart("mix");// alternative
		mp.addBodyPart(mbp1);
		mp.addBodyPart(mbp2);
		msg.setContent(mp);

		msg.setSentDate(new Date());
		
		FileOutputStream output = new FileOutputStream("D:\\t.mail");
		msg.writeTo(output);
		output.flush();
		output.close();
		
		Transport transport = session.getTransport("smtp");
		transport.connect(mailhost, username, password);
		transport.sendMessage(msg, msg.getAllRecipients());
		
		Transport.send(msg);
		System.out.println(mp.getCount());
		System.out.println("\nMail   was   sent   successfully. ");

	}

	static class SMTPAuth extends javax.mail.Authenticator {
		private String user, password;

		public SMTPAuth(String u, String p) {
			user = u;
			password = p;
		}

		public void getuserinfo(String getuser, String getpassword) {
			user = getuser;
			password = getpassword;
		}

		protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
			return new javax.mail.PasswordAuthentication(user, password);
		}

	}

}
